package cn.kogee.basic.interceptor;

import cn.kogee.basic.annotation.PreAuthorize;
import cn.kogee.basic.jwt.JwtUtils;
import cn.kogee.basic.jwt.Payload;
import cn.kogee.basic.jwt.RsaUtils;
import cn.kogee.system.domain.LoginData;
import cn.kogee.system.mapper.PermissionMapper;
import cn.kogee.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.PublicKey;
import java.util.List;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private PermissionMapper permissionMapper;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //从请求头中获取token
        String token = request.getHeader("token");
        if(token!=null){
            //获取公钥
            Logininfo logininfo = null;
            try {
                PublicKey publicKey = RsaUtils.getPublicKey(JwtUtils.class.getClassLoader().getResource("auth_rsa.pub").getFile());
                //解密之后获取载荷数据
                Payload<LoginData> payload = JwtUtils.getInfoFromToken(token, publicKey, LoginData.class);
                logininfo = payload.getLoginData().getLogininfo();
            } catch (Exception e) {//过期时抛出异常
                e.printStackTrace();
                response.setContentType("application/json;charset=UTF-8");
                response.getWriter().println(" {\"success\":false,\"msg\":\"timeout\"}");
                return false;
            }
            if(logininfo.getType()==1) {//如果是用户访问则直接放行
                return true;
            }
                //2.如果是后台管理员 - 验证权限【先获取当前登录人的所有权限sn的集合 ，再获取当前接口方法上的注解里面的sn】
                //强转响应体
                HandlerMethod method = (HandlerMethod)handler;
                //获取接口上的PreAuthorize注解
                PreAuthorize methodAnnotation = method.getMethodAnnotation(PreAuthorize.class);
                if(methodAnnotation==null){
                    //方法上没有注解说明为公共资源直接放行
                    return true;
                }
                //获取注解上的sn
                String sn = methodAnnotation.sn();
                //如有访问的方法有注解则需要验证该用户是否有该权限
                List<String> permission=permissionMapper.loadsnBylogininfoID(logininfo.getId());
                if(permission.contains(sn)){
                //如果查询出来的数据包含sn则放行
                    return true;
                }
                response.setContentType("application/json;charset=UTF-8");
                response.getWriter().println(" {\"success\":false,\"msg\":\"noPermission\"}");
                return false;
            }


        //告诉浏览器返回给前端的是一个json格式的数据
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().println(" {\"success\":false,\"msg\":\"noLogin\"}");
        return false;
    }
}
